package com.lb.shopguide.ui.fragment.guide;

import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.ui.view.NormalTitleBar;

import butterknife.BindView;


/**
 * Created by kang on 2017/4/25.
 */

public class FragmentAgreement extends BaseCommonFragment {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.webview)
    WebView mWebView;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;

    private String url;
    private String title;
    private boolean zoomFlag;

    public static FragmentAgreement newInstance(String title, String url) {
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.WEB_URL, url);
        bundle.putString(AppConstant.WEB_TITLE, title);
        FragmentAgreement fragment = new FragmentAgreement();
        fragment.setArguments(bundle);
        return fragment;
    }

    public static FragmentAgreement newInstance(String title, String url, boolean flag) {
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.WEB_URL, url);
        bundle.putString(AppConstant.WEB_TITLE, title);
        bundle.putBoolean(AppConstant.WEB_ZOOM_FLAG, flag);
        FragmentAgreement fragment = new FragmentAgreement();
        fragment.setArguments(bundle);
        return fragment;
    }


//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container,
//                             Bundle savedInstanceState) {
//        View view = inflater.inflate(R.layout.fragment_webview, container, false);
//        initData();
//        initView(view);
//
//        return view;
//    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_webview;
    }

    @Override
    public void onDetach() {
        super.onDetach();
//        mOnNextStepListener = null;
    }

    @Override
    public void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.WEB_URL)) {
            url = bundle.getString(AppConstant.WEB_URL);
        }
        if (bundle.containsKey(AppConstant.WEB_TITLE)) {
            title = bundle.getString(AppConstant.WEB_TITLE);
        }
        if (bundle.containsKey(AppConstant.WEB_ZOOM_FLAG)) {
            zoomFlag = bundle.getBoolean(AppConstant.WEB_ZOOM_FLAG);
        }
    }

    @Override
    public void initView() {
        mWebView.loadUrl(url);
        // 进度条控制
        mWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (isVisible()) {
                    if (newProgress >= 100) {
                        progressBar.setVisibility(View.GONE);
                    } else {
                        progressBar.setVisibility(View.VISIBLE);
                    }
                    super.onProgressChanged(view, newProgress);
                }
            }

        });
//        mWebView.addJavascriptInterface(getActivity(), "android");
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

        ntb.setLeftImageSrc(R.drawable.iv_back);
        ntb.setTitleText(title);
        ntb.setOnLeftImageListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //弹出
                pop();
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {

    }

    @Override
    public void requestError(String error) {

    }

    @Override
    public String getPageTag() {
        // TODO: 2017/10/14 如何区分不同web页面
        return FragmentAgreement.class.getName();
    }

}
